#include "gameselect.h"
#include"gamehall.h"
#include"gameroom.h"
#include<QIcon>
#include<QPushButton>
#include<QSound>
#include<QPainter>
#include<QPixmap>
#include<QTextEdit>
#include<QFile>
#include<QTextStream>
#include<QDebug>

Gameselect::Gameselect(QWidget *parent) : QWidget(parent)
{
    //窗口大小
    this->setFixedSize(1000,900);
    //设置窗口图标
    this->setWindowIcon(QIcon(":/Snake icon/snake.png"));
    //设置窗口标题
    this->setWindowTitle("选择模式");

    QPushButton* backbutton=new QPushButton(this);//返回按钮
    backbutton->move(900,800);
    backbutton->resize(100,100);
    backbutton->setIcon(QIcon(":/Snake icon/return.png"));

    connect(backbutton,&QPushButton::clicked,[=](){  //返回开始页面
        this->close();
        GameHall* gamehall=new GameHall;
        gamehall->show();
        QSound::play(":/Snake icon/returnsound.wav");
    });


    QFont font("微软雅黑",20);

    GameRoom *room=new GameRoom;

    QPushButton* easybutton=new QPushButton(this);//简单模式按钮
    easybutton->move(400,200);
    easybutton->resize(180,50);
    easybutton->setText("简单模式");
    easybutton->setFont(font);
    connect(easybutton,&QPushButton::clicked,[=](){
        this->close();
        room->setGeometry(this->geometry());
        room->show();
        QSound::play(":/Snake icon/startgame_click.wav");

        room->setMoveSpeed(300);//设置难度
    });

    QPushButton* normalbutton=new QPushButton(this);//普通模式按钮
    normalbutton->move(400,300);
    normalbutton->resize(180,50);
    normalbutton->setText("普通模式");
    normalbutton->setFont(font);
    connect(normalbutton,&QPushButton::clicked,[=](){
        this->close();
        room->setGeometry(this->geometry());
        room->show();
        QSound::play(":/Snake icon/startgame_click.wav");
        room->setMoveSpeed(200);
    });

    QPushButton* hardbutton=new QPushButton(this);//困难模式按钮
    hardbutton->move(400,400);
    hardbutton->resize(180,50);
    hardbutton->setText("困难模式");
    hardbutton->setFont(font);
    hardbutton->setStyleSheet("QPushButton { background-color: rgba(255, 255, 255, 0); }");
    connect(hardbutton,&QPushButton::clicked,[=](){
        this->close();
        room->setGeometry(this->geometry());
        room->show();
        QSound::play(":/Snake icon/startgame_click.wav");

        room->setMoveSpeed(100);
    });

    QPushButton* historybutton=new QPushButton(this);//历史战绩按钮
    historybutton->move(400,500);
    historybutton->resize(180,50);
    historybutton->setText("历史战绩");
    historybutton->setFont(font);
    connect(historybutton,&QPushButton::clicked,[=](){
        QWidget* widget=new QWidget;
        widget->setWindowTitle("历史战绩");
        widget->setFixedSize(500,300);

        //战绩窗口
        QTextEdit* edit=new QTextEdit(widget);
        edit->setFont(font);
        edit->setFixedSize(500,300);

        QFile file("C:/text/sroce.txt.txt");//将文件当中的数据读取出来
        file.open(QIODevice::ReadOnly);

        QTextStream in(&file);
        int data=in.readLine().toInt();
        edit->append("得分为:");
        edit->append(QString::number(data));
        //edit->append(in.readLine());

        QSound::play(":/Snake icon/startgame_click.wav");
        widget->show();
    });


}

void Gameselect::paintEvent(QPaintEvent *event)
{
    //初始化画家
    QPainter painter(this);
    //初始化绘图设备
    QPixmap pixmap(":/Snake icon/select_background.png");

    painter.drawPixmap(0,0,this->width(),this->height(),pixmap);
}
